<?php

namespace App\Http\Controllers\Home;

use App\Admin\Album;
use App\Admin\Categoods;
use App\Admin\Category;
use App\Admin\GetCategory;
use App\Admin\Single;
use App\Home\Special;
use App\Home\SpecialPics;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;

class SpecialController extends Controller {
	public function ToSpecial() {
		//取出session中的用户id
		$user_id = Session()->get('user_id');
		//取出用户的头像
		$head = DB::table('headpics')->where('user_id', $user_id)->select('head')->get()->toArray();
		$head = $head[0]->head;
		//根据用户id获取用户所有的专辑
		$allspecial = Special::where('user_id', $user_id)->get()->toArray();
		$specialnum = DB::table('specials')->where('user_id', $user_id)->where('ban', 1)->select(DB::Raw("count(*)as num"))->get()->toArray();
		$specialnum = $specialnum[0]->num;
		$detailnum = DB::table('specialpics')->where('user_id', $user_id)->select(DB::Raw("count(*)as num"))->get()->toArray();
		$detailnum = $detailnum[0]->num;
		$label = DB::table('label')->get()->toArray();
		return view('home.special')->with(['allspecial' => $allspecial, 'head' => $head, 'specialnum' => $specialnum, 'detailnum' => $detailnum, 'label' => $label]);
	}
//  创建个人专辑
	public function Create(Request $request) {
		//获取前台表单中的专辑名称，标签，描述等等
		$name = $request->name;
		$desc = $request->desc;
		$tag = $request->tag;
		$pic = $request->pic;
		$user_id = Session()->get('user_id');
		$Special = new Special();
		//如果接受的名字为空，就不执行
		if ($name != '') {
			//如果接受的图片为空，就默认给一张图片
			if ($pic == '') {
				$request->pic->move(public_path() . '/public', '1.jpg');

			} else {
				$pic = time() . '.jpg';
				$request->pic->move(public_path() . '/public', $pic);
			}
//           $request->pic->move(public_path() . '/public', $pic);
			//执行插入到数据库的操作
			$Special->user_id = $user_id;
			$Special->name = $name;
			$Special->desc = $desc;
			$Special->tag = $tag;
			$Special->pic = $pic;
			$result = $Special->save();
//           dd($result);
			if ($result == 'true') {
				return redirect('user/special');
			}
		} else {
			return view('home.special');
		}
	}

	//删除个人专辑
	public function Delete($id) {
		$result = Special::where('id', $id)->delete();
		return redirect('user/special');
	}

	//编辑整个专辑，有描述和标签
	public function Edit(Request $request, $id) {
		$special_id = $request->special_id;
		$name = $request->name;
		$desc = $request->desc;
		$tag = $request->tag;
		//执行修改的操作
		$result = DB::table('specials')->where('id', $special_id)->update([
			'name' => $name, 'desc' => $desc, 'tag' => $tag,
		]);
		return back()->with('id', $special_id);
	}

	//查看个人专辑的详细信息,这里的id是专辑的id
	public function Detail($id) {
		$user_id = Session()->get('user_id');
		$head = DB::table('headpics')->where('user_id', $user_id)->select('head')->get()->toArray();
		$head = $head[0]->head;
		$detail = SpecialPics::where('special_id', $id)->get()->toArray();
//       dd($detail);
		$special = Special::where('id', $id)->get()->toArray();
		$special = $special[0];
//       dd($special);
		$tag = $special['tag'];
		$label = DB::table('label')->get()->toArray();
		return view('home.detailspecial')->with(['detail' => $detail, 'head' => $head, 'tag' => $tag, 'special_id' => $id, 'special' => $special, 'label' => $label]);

	}
	//添加个人专辑的详情
	public function AddDetail(Request $request) {
		$user_id = Session()->get('user_id');
		$special_id = $request->input('special_id');
		$desc = $request->input('desc');
		$pic = time() . '.jpg';
		$request->pic->move(public_path() . '/public', $pic);
		$specialpics = new SpecialPics();
		$specialpics->user_id = $user_id;
		$specialpics->desc = $desc;
		$specialpics->special_id = $special_id;
		$specialpics->pics = $pic;
		$result = $specialpics->save();
//        dd($special_id);
		//        使用重定向的话就要类似于拼接路径，把id带到地址栏里
		//        return redirect('user/godetailspecial')->with('id','aaaa');
		return back()->with('id', $special_id);
	}

	//单个专辑内的相片id
	public function DelDetail($id) {
		$specialpic = SpecialPics::where('id', $id)->get()->toArray();
		//专辑的id
		$special_id = $specialpic[0]['special_id'];
//        dd($special_id);
		$result = SpecialPics::where('id', $id)->delete();
//        return redirect('user/godetailspecial')->with('id',$special_id);
		return back()->with('id', $special_id);
	}

	//单个专辑内的相片信息的修改
	public function EditDetail(Request $request) {
		//相片id
		$id = $request->input('id');
		$desc = $request->input('desc');
		$result = DB::table('specialpics')->where('id', $id)->update([
			'desc' => $desc,
		]);
		return back()->with('id', $id);
	}
	//将专辑遍历到首页
	public function ToHome() {
		$album = Album::select()->get();
		$special = DB::table('users')->leftJoin('specials', 'users.id', '=', 'specials.user_id')
			->where('ban', 1)->limit(5)->get()->toArray();
		$search = DB::table('daren')
			->leftJoin('users', 'daren.users_id', '=', 'users.id')
			->leftJoin('headpics', 'daren.users_id', '=', 'headpics.user_id')
			->limit(5)->get()->toArray();
//        dd($search);
		$lianggoods = DB::table('lianggoods')
			->where('recommend', 1)->limit(5)->get()->toArray();
		$categorylist = DB::table('category')
			->where('disstatus', 1)->get()->toArray();
		$cid = isset($_GET['cid']) ? $_GET['cid'] : '';
		$ccid = isset($_GET['ccid']) ? $_GET['ccid'] : '';
		if ($cid == '') {
//            dd(123);
			$single = Single::select()->where('disstatus', 1)->get();
		} else {
			$Cate = Category::select()->where('disstatus', 1)->get();
			$getCate = new GetCategory();
			$id = $getCate->getChildId($Cate, $cid);
			if (implode(',', $id) !== '') {
				//单品的分类
				foreach ($id as $val) {
					$list[] = DB::table('single')
						->where('disstatus', 1)
						->where('category_id', $val)
						->get()->toArray();
				}
				foreach ($list as $val) {
					foreach ($val as $v) {
						$single[] = $v;
					}
				}
			} else {

				return back()->with('cid', $ccid);

			}
		}
		$music = DB::table('music')->get()->toJson();
		return view('home.homepage', compact('album', 'single', 'special', 'search', 'lianggoods', 'categorylist', 'music'));
	}

	public function ToHope(Request $request) {
		$data = $request->all();
		//CURL
		//初始化curl
		$curl = curl_init();
// var_dump($curl);
		// 设置APIKEY   URL
		$apikey = "7d4eeeea0de64fb9b227aaab6b19a8f7";
		$word = urlencode($data['data']); //%E5%B0%8F%E7%B1%B3
		// http://www.tuling123.com/openapi/api?key=KEY&info=北京中关村附近的酒店
		//CURL配置
		curl_setopt($curl, CURLOPT_URL, 'http://www.tuling123.com/openapi/api?key=' . $apikey . '&info=' . $word);
//将curl_exec()获取的信息 以文件流的形式,而不是直接输出的形式得到
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//curl执行
		$data = curl_exec($curl);

//关闭curl
		curl_close($curl);

//处理JSON数据
		// var_dump($data);
		return $data;
//提取文章信息
		// $newslist = $jsonObj->newslist;
		// var_dump($newslist);

	}
	//将首页良品购遍历到详情页
	public function ToDangoods() {
		$uid = session()->get('user_id');
//        dd($uid);
		//接收单品id查单品表
		$id = $_GET['id'];
		$dangoods = DB::table('single')
			->where('id', $id)->where('disstatus', 1)->get()->toArray();
		//查收藏数
		$danshou = DB::table('collect')
			->where('single_id', $id)->where('collectstatus', 1)
			->count();
		$shou = DB::table('collect')
			->where('single_id', $id)->where('users_id', $uid)->where('collectstatus', 1)
			->count();
		if ($shou == 0) {
			$shou = '';
		}
		$zan = DB::table('zan')
			->where('single_id', $id)->where('users_id', $uid)->where('zanstatus', 1)
			->count();
//        dd($zan);
		if ($zan == 0) {
			$zan = '';
		}
		//评论展示

		$ping = DB::table('ping')->where('single_id', $id)->count();
//        dd($ping);
		//        dd($rec);
		if ($ping != 0) {
			$rec = DB::table('ping')->where('single_id', $id)->get();
			$user = DB::table('users')->where('id', $uid)->get();
			return view('home.dandetail')->with(['dangoods' => $dangoods, 'danshou' => $danshou, 'shou' => $shou, 'zan' => $zan, 'rec' => $rec, 'user' => $user, 'ping' => $ping]);
		} else {
			$rec = null;
			//$user = null;
		}
		return view('home.dandetail')->with(['dangoods' => $dangoods, 'danshou' => $danshou, 'shou' => $shou, 'zan' => $zan, 'rec' => $rec]);
	}
	//单品收藏状态总数更改
	public function shoudata() {
		$uid = session()->get('user_id');
		$id = $_GET['id'];
		$shou = $_GET['shou'];

//        dd($_GET);
		if ($shou == 1) {
			$shou = DB::table('collect')
				->where('users_id', $uid)
				->update(['collectstatus' => 2, 'single_id' => $id, 'users_id' => $uid]);
		} else {
			$shou = DB::table('collect')
				->where('users_id', $uid)
				->insert(['collectstatus' => 1, 'single_id' => $id, 'users_id' => $uid]);
		}

		return redirect('home/dandetail?id= ' . $id);
	}
	//单品点赞状态总数更改
	public function zandata() {
		$uid = session()->get('user_id');
		$id = $_GET['id'];
		$zan = $_GET['zan'];

//        dd($_GET);
		if ($zan == 1) {
			$zan = DB::table('zan')
				->where('users_id', $uid)
				->update(['zanstatus' => 2, 'single_id' => $id, 'users_id' => $uid]);
		} else {
			$zan = DB::table('zan')
				->where('users_id', $uid)
				->insert(['zanstatus' => 1, 'single_id' => $id, 'users_id' => $uid]);
		}

		return redirect('home/dandetail?id=' . $id);
	}
	//将首页良品购遍历到详情页
	public function ToLianggoods() {
		$id = $_GET['id'];
		$lianggoods = DB::table('lianggoods')
			->where('id', $id)->get()->toArray();
		return view('home.liangdetail')->with(['lianggoods' => $lianggoods]);
	}
	//遍历良品购列表
	public function ToLianglist() {
		$ccid = isset($_GET['ccid']) ? $_GET['ccid'] : '';
//        dd($ccid);
		$cid = isset($_GET['cid']) ? $_GET['cid'] : '';
		if ($cid) {
			$Cate = Categoods::all();
//            $Liang=Lianggoods::all();
			$getCate = new GetCategory();
			$id = $getCate->getChildId($Cate, $cid);
//            dd(implode(',',$id));
			if (implode(',', $id) !== '') {

				//良品购的分类
				foreach ($id as $val) {
					$list[] = DB::table('categoods')
						->where('disstatus', 1)->where('id', $val)->get()->toArray();
				}
				foreach ($list as $val) {
					$lianglist[] = $val[0];
				}
			} else {
//                dd($ccid);

				return back()->with('cid', $ccid);
			}
//            dd($list);
			//            dd($id);
			//                 良品购商品
			foreach ($id as $value) {
				$liang[] = DB::table('lianggoods')
					->where('disstatus', 1)->where('categoods_id', $value)->get()->toArray();
			}
//            dd($liang);
			foreach ($liang as $value) {

				foreach ($value as $v) {
					$lianggoods[] = $v;
				}
			}
//            dd($lianggoods);

		} else {
			//良品购的分类s
			$lianglist = DB::table('categoods')
				->where('disstatus', 1)->where('pid', '=', 0)->get()->toArray();
			$lianggoods = DB::table('lianggoods')
				->where('disstatus', 1)->get()->toArray();
		}
		return view('home.liangfashion')->with(['lianglist' => $lianglist, 'lianggoods' => $lianggoods]);
	}
	//星座运势页
	public function xingzuo(Request $request) {
//        dd($request->all());
		//        dump($jsonObj);
		//取出session中的用户id
		$user_id = Session()->get('user_id');
		//取出用户的头像
		$head = DB::table('headpics')->where('user_id', $user_id)->select('head')->get()->toArray();
		$head = $head[0]->head;
		//根据用户id获取用户所有的专辑
		$allspecial = Special::where('user_id', $user_id)->get()->toArray();
		//////////////////////////////////////////////////////////////
		$xing = isset($request->all()['xingzuo']) ? $request->all()['xingzuo'] : '';
//        dd($xing);
		//初始化curl
		$curl = curl_init();
		// var_dump($curl);
		// 设置APIKEY   URL
		$apikey = "e8f96d3b446e43a3b282cb9ee8c2c1bd";
		$consName = urlencode($xing); //%E5%B0%8F%E7%B1%B3

		// $w = urlencode('北京市海淀区信息路28号');//%E5%B0%8F%E7%B1%B3
		$type = 'today'; //%E5%B0%8F%E7%B1%B3
		// http://www.tuling123.com/openapi/api?key=KEY&info=北京中关村附近的酒店
		//CURL配置
		curl_setopt($curl, CURLOPT_URL, 'http://apis.haoservice.com/lifeservice/constellation/GetAll?key=' . $apikey . '&consName=' . $consName . '&type=' . $type);
		//将curl_exec()获取的信息 以文件流的形式,而不是直接输出的形式得到
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

		//curl执行
		$data = curl_exec($curl);

		//关闭curl
		curl_close($curl);

		//处理JSON数据
		$jsonObj = json_decode($data);
		if ($jsonObj->error_code == 203201) {
			$jsonObj = '';
		}
//        dd($jsonObj);
		return view('home.xingzuo')->with(['allspecial' => $allspecial, 'head' => $head, 'jsonObj' => $jsonObj]);
	}
	//遍历帮帮中心
	public function help() {
		$help = DB::table('help')->get()->toArray();
//        dd($help);
		return view('home.help')->with(['help' => $help]);
	}
	//链接联系我们
	public function mail() {
		return view('home.mail');
	}
	//处理提交的数据发送邮箱
	public function maildata(Request $request) {
		//验证规则
		$roles = [
			'name' => 'required|between:1,10',
			'describe' => 'required|between:5,150',
		];
		//自定义的错误信息
		$msg = [
			'name.required' => '名称不能为空',
			'name.between' => '名称必须在:min和:max之间',
			'describe.required' => '内容不能为空',
			'describe.between' => '内容必须在:min和:max之间',
		];
		$validator = Validator::make($request->all(), $roles, $msg);
		if ($validator->fails()) {
			//验证失败
			return redirect('home/mail')->withErrors($validator);
		}
		$data = $request->all();
		$name = $data['name'];
		$describe = $data['describe'];
//        dump($name);
		//        dump($describe);
		//        dd($data);
		Mail::send('home.didi', $data, function ($message) use ($data) {
			$message->subject('xdlduitang.com')
				->to('1958329399@qq.com');
		});
		return redirect('home/homepage');
	}
	public function ets(Request $request) {
		if ($request->isMethod('post')) {
			$data = $request->all();
			//CURL
			//初始化curl
			$curl = curl_init();
// var_dump($curl);
			// 设置APIKEY   URL
			$apikey = "aafe3c4faf4b40fbb395e73eac0cfcd1";
			$word = urlencode($data['data']); //%E5%B0%8F%E7%B1%B3
			// http://www.tuling123.com/openapi/api?key=KEY&info=北京中关村附近的酒店
			//CURL配置
			curl_setopt($curl, CURLOPT_URL, 'http://www.tuling123.com/openapi/api?key=' . $apikey . '&info=' . $word);
//将curl_exec()获取的信息 以文件流的形式,而不是直接输出的形式得到
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//curl执行
			$data = curl_exec($curl);

//关闭curl
			curl_close($curl);

//处理JSON数据
			// var_dump($data);
			return $data;
//提取文章信息
			// $newslist = $jsonObj->newslist;
			// var_dump($newslist);
		} else {
			return view('home.tuling');
		}
	}
}
